package cn.stylefeng.guns.modular.headers.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.headers.entity.QynPfcHeaders;
import cn.stylefeng.guns.modular.headers.enums.QynPfcHeadersExceptionEnum;
import cn.stylefeng.guns.modular.headers.mapper.QynPfcHeadersMapper;
import cn.stylefeng.guns.modular.headers.pojo.request.QynPfcHeadersRequest;
import cn.stylefeng.guns.modular.headers.service.QynPfcHeadersService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 交易中心自动获取headers业务实现层
 *
 * @author 我是谭先生🐻
 * @date 2025/03/12 11:22
 */
@Service
public class QynPfcHeadersServiceImpl extends ServiceImpl<QynPfcHeadersMapper, QynPfcHeaders> implements QynPfcHeadersService {

	@Override
    public void add(QynPfcHeadersRequest qynPfcHeadersRequest) {
        QynPfcHeaders qynPfcHeaders = new QynPfcHeaders();
        BeanUtil.copyProperties(qynPfcHeadersRequest, qynPfcHeaders);
        this.save(qynPfcHeaders);
    }

    @Override
    public void del(QynPfcHeadersRequest qynPfcHeadersRequest) {
        QynPfcHeaders qynPfcHeaders = this.queryQynPfcHeaders(qynPfcHeadersRequest);
        this.removeById(qynPfcHeaders.getHeadersId());
    }

    @Override
    public void edit(QynPfcHeadersRequest qynPfcHeadersRequest) {
        QynPfcHeaders qynPfcHeaders = this.queryQynPfcHeaders(qynPfcHeadersRequest);
        BeanUtil.copyProperties(qynPfcHeadersRequest, qynPfcHeaders);
        this.updateById(qynPfcHeaders);
    }

    @Override
    public QynPfcHeaders detail(QynPfcHeadersRequest qynPfcHeadersRequest) {
        return this.queryQynPfcHeaders(qynPfcHeadersRequest);
    }

    @Override
    public PageResult<QynPfcHeaders> findPage(QynPfcHeadersRequest qynPfcHeadersRequest) {
        LambdaQueryWrapper<QynPfcHeaders> wrapper = createWrapper(qynPfcHeadersRequest);
        Page<QynPfcHeaders> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<QynPfcHeaders> findList(QynPfcHeadersRequest qynPfcHeadersRequest) {
        LambdaQueryWrapper<QynPfcHeaders> wrapper = this.createWrapper(qynPfcHeadersRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 我是谭先生🐻
     * @date 2025/03/12 11:22
     */
    private QynPfcHeaders queryQynPfcHeaders(QynPfcHeadersRequest qynPfcHeadersRequest) {
        QynPfcHeaders qynPfcHeaders = this.getById(qynPfcHeadersRequest.getHeadersId());
        if (ObjectUtil.isEmpty(qynPfcHeaders)) {
            throw new ServiceException(QynPfcHeadersExceptionEnum.QYUAN_PFC_HEADERS_NOT_EXISTED);
        }
        return qynPfcHeaders;
    }

    /**
     * 创建查询wrapper
     *
     * @author 我是谭先生🐻
     * @date 2025/03/12 11:22
     */
    private LambdaQueryWrapper<QynPfcHeaders> createWrapper(QynPfcHeadersRequest qynPfcHeadersRequest) {
        LambdaQueryWrapper<QynPfcHeaders> queryWrapper = new LambdaQueryWrapper<>();

        Long headersId = qynPfcHeadersRequest.getHeadersId();
        String url = qynPfcHeadersRequest.getUrl();
        String cookie = qynPfcHeadersRequest.getCookie();
        String csrftoken = qynPfcHeadersRequest.getCsrftoken();

        queryWrapper.eq(ObjectUtil.isNotNull(headersId), QynPfcHeaders::getHeadersId, headersId);
        queryWrapper.like(ObjectUtil.isNotEmpty(url), QynPfcHeaders::getUrl, url);
        queryWrapper.like(ObjectUtil.isNotEmpty(cookie), QynPfcHeaders::getCookie, cookie);
        queryWrapper.like(ObjectUtil.isNotEmpty(csrftoken), QynPfcHeaders::getCsrftoken, csrftoken);

        return queryWrapper;
    }

}